//
//  CustomTextField.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/27/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit



protocol CustomTFProtocol {
    var textField: UITextField { get set }
    var placeholderLbl: UILabel { get set }
}


final class PlaceholderTF: UIView, CustomTFProtocol {
    lazy var textField: UITextField = {
        let field = UITextField()
        field.backgroundColor = .clear
        field.borderStyle = .none
        field.font = UIFont.systemFont(ofSize: 17, weight: .medium)
        field.textColor = .darkBlueColor
        return field
    }()
    lazy var placeholderLbl: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12, weight: .regular)
        label.textAlignment = .left
        label.sizeToFit()
        label.numberOfLines = 1
        label.textColor = .greyColor
        return label
    }()
    
    init (placeholder: String, text: String, kType: UIKeyboardType) {
        super.init(frame: .zero)
        self.addView()
        self.placeholderLbl.text = placeholder
        
        self.textField.placeholder = text
        self.textField.keyboardType = kType
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func addView() {
        self.addSubview(textField)
        self.addSubview(placeholderLbl)
        
        self.backgroundColor = .whiteBlueColor
        self.clipsToBounds = true
        self.layer.cornerRadius = 7
        
        placeholderLbl.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(12)
            make.top.equalToSuperview().offset(6)
            make.height.equalTo(24)
        }
        textField.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().inset(6)
            make.leading.trailing.equalToSuperview().inset(12)
            make.height.equalTo(24)
        }
    }
}



class PaddingTextField: UITextField {
    
    var padding = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
    
    override open func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }
    override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }
    override open func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }
}
